<?php

/**
 * This is the model class for table "{{comment}}".
 *
 * The followings are the available columns in table '{{comment}}':
 * @property string $com_id
 * @property integer $com_aid
 * @property integer $com_pid
 * @property integer $com_rid
 * @property integer $com_auid
 * @property ubteger $com_mid
 * @property integer $com_gid
 * @property integer $com_vid
 * @property integer $com_fid
 * @property interger $com_proid
 * @property interger $com_solid
 * @property integer $com_status
 * @property string $com_author
 * @property string $com_email
 * @property string $com_website
 * @property integer $com_ip
 * @property string $com_contents
 * @property integer $com_create
 */
class Comment extends CActiveRecord
{
	
	const COMMENT_PUBLISHED = 1;
	const COMMENT_TRASH = 2;
	const COMMENT_LOCK = 3;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Comment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{comment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('com_author, com_email, com_contents,', 'required'),
			array('com_aid, com_auid, com_rid, com_pid, com_gid, com_vid, com_fid, com_proid, com_solid, com_status, com_ip, com_create', 'numerical', 'integerOnly'=>true),
			array('com_author', 'length', 'max'=>30),
			array('com_email, com_website', 'length', 'max'=>200),
            array('com_email','email'),
            array('com_website','url'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('com_id, com_aid, com_pid, com_mid, com_rid, com_auid,  com_gid, com_vid, com_fid, com_proid, com_solid, com_status, com_author, com_email, com_website, com_ip, com_contents, com_create', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'article'=>array(self::BELONGS_TO, 'Article', 'com_aid'),
			'media'=>array(self::BELONGS_TO, 'Media', 'com_mid'),
			'video'=>array(self::BELONGS_TO, 'File', 'com_vid'),
			'audio'=>array(self::BELONGS_TO, 'File', 'com_auid'),
			'file' => array(self::BELONGS_TO, 'File', 'com_fid'),
			'comments'=>array(self::HAS_MANY, 'Comment', 'com_rid'),
			'reply'=>array(self::BELONGS_TO, 'Comment', 'com_rid'),
			'project'=>array(self::BELONGS_TO, 'Project', 'com_proid'),
            'solution'=>array(self::BELONGS_TO, 'Solution', 'com_solid')
			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'com_id' => Yii::t('comment','ID'),
			'com_aid' => Yii::t('comment','Article ID'),
			'com_auid' => Yii::t('comment','Audio ID'),
			'com_mid' => Yii::t('commend', 'Media ID'),
			'com_pid' => Yii::t('comment','Parent ID'),
			'com_gid' => Yii::t('comment','Gallery ID'),
			'com_vid' => Yii::t('comment','Video ID'),
			'com_fid' => Yii::t('comment','File ID'),
			'com_rid' => Yii::t('comment','Reply ID'),
            'com_proid' => Yii::t('comment','Project ID'),
            'com_solid' => Yii::t('comment','Solution ID'),
			'com_status' => Yii::t('comment','Status'),
			'com_author' => Yii::t('comment','Author'),
			'com_email' => Yii::t('comment','Email'),
			'com_website' => Yii::t('comment','Website'),
			'com_ip' => Yii::t('comment','IP'),
			'com_contents' => Yii::t('comment','Contents'),
			'com_create' => Yii::t('comment','Create'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('com_id',$this->com_id,true);
		$criteria->compare('com_aid',$this->com_aid);
		$criteria->compare('com_auid',$this->com_auid);
		$criteria->compare('com_mid',$this->com_mid);
		$criteria->compare('com_pid',$this->com_pid);
		$criteria->compare('com_gid',$this->com_gid);
		$criteria->compare('com_vid',$this->com_vid);
		$criteria->compare('com_fid',$this->com_fid);
        $criteria->compare('com_solid', $this->com_solid);
		$criteria->compare('com_proid', $this->com_proid);
		$criteria->compare('com_status',$this->com_status);
		$criteria->compare('com_author',$this->com_author,true);
		$criteria->compare('com_email',$this->com_email,true);
		$criteria->compare('com_website',$this->com_website,true);
		$criteria->compare('com_ip',$this->com_ip);
		$criteria->compare('com_contents',$this->com_contents,true);
		$criteria->compare('com_create',$this->com_create);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getCommentState($state)
	{
		switch ($state) {
			case self::COMMENT_PUBLISHED:
			 	return Yii::t('common','PUBLISHED');
				break;
			case self::COMMENT_LOCK:
				return Yii::t('common','LOCK');
				break;
			case self::COMMENT_TRASH:
				return Yii::t('common','TRASH');
				break;
		}
	}
	
	public function generateCommentStateDropdownList()
	{
		return array(
			self::COMMENT_PUBLISHED => Yii::t('common','PUBLISHED'),
			self::COMMENT_LOCK => Yii::t('common','LOCK'),
			self::COMMENT_TRASH => Yii::t('common','TRASH')
		);
	}
	
	public function getCommentResourceTitle($model)
	{
		if($model->com_aid)
			return $model->article->arc_title;
		elseif ($model->com_mid)
			return $model->media->media_name;
		elseif ($model->com_vid)
			return $model->video->file_name;
		elseif ($model->com_auid)
			return $model->audio->file_name;
		elseif ($model->com_fid)
			return $model->file->file_name;
		elseif ($model->com_gid)
			return $model->gallery->file_name;
        elseif ($model->com_proid)
            return $model->project->pro_title;
		else 
			return '无';
		
	}
	
	public function getCommentResourceIsComment($model)
	{
		if($model->com_aid)
			return $model->article->arc_iscomment;
		elseif ($model->com_mid)
			return $model->media->media_iscomment;
		elseif ($model->com_vid)
			return $model->video->file_iscomment;
		elseif ($model->com_auid)
			return $model->audio->file_iscomment;
		elseif ($model->com_fid)
			return $model->file->file_iscomment;
		elseif ($model->com_gid)
			return $model->gallery->file_iscomment;	
	}
	
	
	public function getCommentResourceStatus($model)
	{
		if($model->com_aid)
			return Article::model()->getCommentState($model->article->arc_iscomment);
		elseif ($model->com_mid)
			return Product::model()->getCommentState($model->media->media_iscomment);
		elseif ($model->com_vid)
			return File::model()->getFileCommentStatus($model->video->file_iscomment);
		elseif ($model->com_auid)
			return File::model()->getFileCommentStatus($model->audio->file_iscomment);
		elseif ($model->com_fid)
			return File::model()->getFileCommentStatus($model->file->file_iscomment);
		elseif ($model->com_gid)
			return File::model()->getFileCommentStatus($model->gallery->file_iscomment);

		
	}
	
	//获取第一层评论
	public function getComments($model)
	{
		if($model->com_aid)
		{
//			$article = Article::model()->findByPk($model->com_aid);
//			return $article->comment;
			
			return self::model()->findAll(array(
				'condition' => 'com_aid =:com_aid AND com_rid IS NULL',
				'params' => array(
					':com_aid' => $model->com_aid
				)
				
			));
		}
		elseif ($model->com_mid)
		{
//			$product = Product::model()->findByPk($model->com_mid);
//			return $product->comment;
			return self::model()->findAll(array(
				'condition' => 'com_mid =:com_mid AND com_rid IS NULL',
				'params' => array(
					':com_mid' => $model->com_mid
				)
			));
		}
		elseif ($model->com_vid)
		{
			return self::model()->findAll(array(
				'condition' => 'com_vid =:com_vid AND com_rid IS NULL',
				'params' => array(
					':com_vid' => $model->com_vid
				)
			));
		}
		elseif ($model->com_auid)
		{
			return self::model()->findAll(array(
				'condition' => 'com_auid =:com_auid AND com_rid IS NULL',
				'params' => array(
					':com_auid' => $model->com_auid
				)
			));
		}
		elseif ($model->com_fid)
		{
			return self::model()->findAll(array(
				'condition' => 'com_fid =:com_fid AND com_rid IS NULL',
				'params' => array(
					':com_fid' => $model->com_fid
				)
			));
		}
		elseif ($model->com_gid)
		{
			return self::model()->findAll(array(
				'condition' => 'com_gid =:com_gid AND com_rid IS NULL',
				'params' => array(
					':com_gid' => $model->com_gid
				)
			));
		}

	}
	
	
	//获取回复交流评论
	public function getCommentsReply($model)
	{
		
	}
	
	//生成回复时所需要文本框的ID
	public function generateTextareaId($model)
	{
		$id = '';
		$separator = '-';  
		$id = 'id:'.$model->com_id.$separator;
		
		if($model->com_aid)
			$id .= 'aid:'.$model->com_aid.$separator;
		elseif ($model->com_mid)
			$id .= 'mid:'.$model->com_mid.$separator;
		elseif ($model->com_fid)
			$id .= 'fid:'.$model->com_fid.$separator;
		elseif ($model->com_auid)
			$id .= 'auid:'.$model->com_auid.$separator;
		elseif ($model->com_vid)
			$id .= 'vid:'.$model->com_vid.$separator;
			
		return $id;
	}
	
	public function getCommentIco($model,$htmlOption=array('width'=>60))
	{
		$path = '/public/images/comment/';
		
		if($model->com_aid)
			return CHtml::image($path.'article.png',$model->article->arc_title,$htmlOption);
		elseif ($model->com_mid)
			return CHtml::image($path.'product.png',$model->media->media_name,$htmlOption);
		else
			return CHtml::image($path.'default.png',$model->com_author,$htmlOption);
	}
	
	public function getStatisticsInfoByCategory($category = '')
	{
		$criteria = new CDbCriteria();
		
		if($category){
			$criteria->condition = $category.' IS NOT NULL';
		}		
	
		return self::model()->count($criteria);
		
	}
	
	public function getItemStatistics($model)
	{
		$count = 0;
	}
	
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->com_create=time();
				$this->com_ip = ip2long(UtilNet::getClientIp());
                
			}
			
			return true;
		}
		else
			return false;
	}
}